package chapter08;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Slf4j
public class CountDownLatchTest_wangzhe {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
        String[] all=new String[10];
        Random random = new Random();
        CountDownLatch latch = new CountDownLatch(10);
        for(int j=0;j<10;j++){
            int k=j;
            pool.submit(()->{
                for(int i=0;i<=100;i++){
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[k]=i+"%";
                    System.out.print("\r"+Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戏开始...");
        pool.shutdown();
    }
}
